package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.FailureCauseDao;
import cn.b.sky.mes.em.model.FailureCause;
import cn.b.sky.mes.em.model.condition.FailureCauseCondition;
import cn.b.sky.mes.em.service.FailureCauseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("failureCauseService")
public class FailureCauseServiceImpl implements FailureCauseService {

  private static final Logger LOG = LoggerFactory.getLogger(FailureCauseServiceImpl.class);

  @Resource
  FailureCauseDao failureCauseDao;

  @Override
  public List<FailureCause> selectFailureCauseListByCondition(FailureCauseCondition condition) {
    if (condition == null) {
      condition = new FailureCauseCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return failureCauseDao.selectFailureCauseListByCondition(condition);
  }

  @Override
  public int selectFailureCauseCount(FailureCauseCondition condition) {
    if (condition == null) {
      condition = new FailureCauseCondition();
    }
    return failureCauseDao.selectFailureCauseCount(condition);
  }

  @Override
  public void updateFailureCause(FailureCause failureCause) {
    if (failureCause == null || StringUtils.isBlank(failureCause.getFailureCauseCode())) {
      return;
    }
    failureCauseDao.updateFailureCause(failureCause);

  }

  @Override
  public void saveFailureCause(FailureCause failureCause) {
    if (failureCause == null || StringUtils.isBlank(failureCause.getFailureCauseCode())) {
      return;
    }
    failureCauseDao.saveFailureCause(failureCause);

  }

  @Override
  public BatchJobReport deleteFailureCause(String failureCauseCode, String siteCode) {
    if (!StringUtils.isBlank(failureCauseCode)) {
      String message = failureCauseCode;
      String cause = "";
      boolean success = true;
      try {
        failureCauseDao.deleteFailureCause(failureCauseCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "failureCause is null", false);
    }
  }

  @Override
  public FailureCause getFailureCause(String causeCode, String siteCode) {
    if (StringUtils.isBlank(causeCode)) {
      return null;
    }
    return failureCauseDao.getFailureCause(causeCode, siteCode);
  }

}
